//服务端钩子，在服务端执行请求之前执行
import type { Handle } from '@sveltejs/kit';

export const handle: Handle = async ({ event, resolve }) => {
	const token = event.cookies.get('dhzy_token');
	const path = event.url.pathname;
    const publicPrefix = ['/login','/component'];	

	/* 1. 未登录 → 只能访问公开页 */
	if (!token && !publicPrefix.some(p => path.startsWith(p))) {
		return Response.redirect(
			`${event.url.origin}/login`,
			302
		);
	}
	/* 2. 已登录 → 禁止再进登录页 */
	if (token && (path === '/' || path === '/login')) {
		return Response.redirect(`${event.url.origin}/home`, 302);
	}

	/* 3. 正常响应 */
	return resolve(event);
};
